package com.ailot.cloud.demo.controller;

import com.ailot.cloud.base.common.dto.Result;
import com.ailot.cloud.demo.entity.DemoUser;
import com.ailot.cloud.demo.service.DemoUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class DemoUserController {

    private final DemoUserService demoUserService;

    @GetMapping("/page")
    @PreAuthorize("hasAuthority('demo:user:index')")
    public Result<IPage> page(Page page, DemoUser demoUser) {
        return Result.success(demoUserService.page(page, Wrappers.query(demoUser)));
    }


    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('demo:user:get')")
    public Result<DemoUser> get(@PathVariable String id) {
        return Result.success(demoUserService.getById(id));
    }


    @PostMapping
    @PreAuthorize("hasAuthority('demo:user:add')")
    public Result<?> add(@RequestBody DemoUser demoUser) {
        return Result.success(demoUserService.save(demoUser));
    }


    @PutMapping
    @PreAuthorize("hasAuthority('demo:user:edit')")
    public Result<?> update(@RequestBody DemoUser demoUser) {
        return Result.success(demoUserService.updateById(demoUser));
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('demo:user:del')")
    public Result<?> delete(@PathVariable String id) {
        return Result.success(demoUserService.removeById(id));
    }


}
